package com.nxy.cpfsfe.util.zookeeper;

import com.adtec.starring.respool.ResPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ZookeeperLockUtil {
	private static final Logger logger = LoggerFactory.getLogger(DistributedLock.class);

	//zookeeper地址
	private String config;
	//竞争资源标志,lockName中不能包含单词lock
	private String lockName;
	private  DistributedLock distributedLock;
	private static ZookeeperLockUtil instance = new ZookeeperLockUtil();
	private ZookeeperLockUtil(){}
	
	public static ZookeeperLockUtil  getInstance(){
		return instance;
	}
	
	
	/**
	 * 获取zookeepe锁操作对象
	 * @param lockName
	 * @return
	 */
	public  DistributedLock creaZooKeeperLock(String lockName){
			//查找主机地址
		String dubboserveraddress = ResPool.configMap.get("dubboserveraddress");
		logger.debug("dubboserveraddress:{}",dubboserveraddress);

		config = dubboserveraddress.split("//")[1];
		config = config.replace("?backup=",",");
		logger.debug("zk_config:{}",config);

		//根据主机获取锁对象
		distributedLock = new DistributedLock(config, lockName);

		return distributedLock;
	}
	
}
